package com.sixbro.shiro.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixbro.shiro.common.utils.ApiResult;
import com.sixbro.shiro.domain.entity.SysFiles;
import com.sixbro.shiro.service.sys.SysFilesService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 文件上传
 * </p>
 *
 * @Author: Mr.Lu
 * @Since: 2020/6/19 15:48
 */
@RestController
@RequestMapping("/sysFiles")
public class SysFilesController {
    @Autowired
    private SysFilesService sysFilesService;

    @ApiOperation(value = "新增")
    @PostMapping("/upload")
    @RequiresPermissions(value = {"sysFiles:add", "sysContent:update", "sysContent:add"}, logical = Logical.OR)
    public ApiResult add(@RequestParam(value = "file") MultipartFile file) {
        //判断文件是否空
        if (file == null || file.getOriginalFilename() == null || "".equalsIgnoreCase(file.getOriginalFilename().trim())) {
            return ApiResult.fail("文件为空");
        }
        return sysFilesService.saveFile(file);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    @RequiresPermissions("sysFiles:delete")
    public ApiResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysFilesService.removeByIdsAndFiles(ids);
        return ApiResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("/listByPage")
    @RequiresPermissions("sysFiles:list")
    public ApiResult findListByPage(@RequestBody SysFiles sysFiles) {
        Page page = new Page(sysFiles.getPage(), sysFiles.getLimit());
        LambdaQueryWrapper<SysFiles> queryWrapper = new LambdaQueryWrapper();
        //查询条件示例
        queryWrapper.orderByDesc(SysFiles::getCreateDate);
        IPage<SysFiles> iPage = sysFilesService.page(page, queryWrapper);
        return ApiResult.success(iPage);
    }
}